package com.cl.code.common.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * 时间工具类
 *
 * @author chengliang
 * @since 1.0.0
 */
public class TimeUtils {

    /**
     * 时间格式
     */
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM = "yyyy-MM";
    public static final String YYYY = "yyyy";

    /**
     * 获取当前时间戳
     *
     * @return 当前时间戳
     */
    public static Long now() {
        return ZonedDateTime.now().toInstant().toEpochMilli();
    }

    /**
     * 获取当天开始时间戳
     *
     * @return 当天开始时间戳
     */
    public static Long today() {
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        LocalDate now = zonedDateTime.toLocalDate();
        LocalDateTime startOfDay = now.atStartOfDay();
        return startOfDay.toInstant(zonedDateTime.getOffset()).toEpochMilli();
    }

    /**
     * 获取昨天开始时间戳
     *
     * @return 昨天开始时间戳
     */
    public static Long yesterday() {
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        LocalDate now = zonedDateTime.toLocalDate();
        LocalDate yesterday = now.minusDays(1);
        LocalDateTime startOfDay = yesterday.atStartOfDay();
        return startOfDay.toInstant(zonedDateTime.getOffset()).toEpochMilli();
    }

    /**
     * 获取明天开始时间戳
     *
     * @return 明天开始时间戳
     */
    public static Long tomorrow() {
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        LocalDate now = zonedDateTime.toLocalDate();
        LocalDate tomorrow = now.plusDays(1);
        LocalDateTime startOfDay = tomorrow.atStartOfDay();
        return startOfDay.toInstant(zonedDateTime.getOffset()).toEpochMilli();
    }

    /**
     * 格式化时间戳
     *
     * @param timestamp 时间戳
     * @param pattern   时间格式
     * @return 时间字符串
     */
    public static String format(Long timestamp, String pattern) {
        LocalDateTime time = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return time.format(formatter);
    }

    /**
     * 格式化时间戳
     *
     * @param timestamp 时间戳
     * @return 时间字符串
     */
    public static String format(Long timestamp) {
        return format(timestamp, YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 解析时间字符串
     *
     * @param time    时间字符串
     * @param pattern 时间格式
     * @return 时间戳
     */
    public static Long parse(String time, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        LocalDateTime localDateTime = LocalDateTime.parse(time, formatter);
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 计算两个时间戳之间的时间间隔
     *
     * @param start 开始时间戳
     * @param end   结束时间戳
     * @return 时间间隔
     */
    public static Duration duration(Long start, Long end) {
        return Duration.between(Instant.ofEpochMilli(start), Instant.ofEpochMilli(end));
    }

}
